package com.secf.service.port.hpy.hys.test.day5;

/**
 * @Author luoYong
 * @Date 2024-08-13 17:40
 */
public class InventoryManager {
    public static void main(String[] args) {
        int[] inventory = {50, 20, 15, 30, 10};

        addStock(inventory, 2, 5);
        System.out.println("Updated Inventory: " + arrayToString(inventory));

        removeStock(inventory, 3, 10);
        System.out.println("Updated Inventory: " + arrayToString(inventory));

        int stock = checkStock(inventory, 1);
        System.out.println("Stock for item 2: " + stock);
    }

    public static void addStock(int[] inventory, int index, int amount) {
        if (index >= 0 && index < inventory.length) {
            inventory[index] += amount;
        }
    }

    public static void removeStock(int[] inventory, int index, int amount) {
        if (index >= 0 && index < inventory.length) {
            inventory[index] -= amount;
            if (inventory[index] < 0) {
                inventory[index] = 0;
            }
        }
    }

    public static int checkStock(int[] inventory, int index) {
        if (index >= 0 && index < inventory.length) {
            return inventory[index];
        }
        return -1;
    }

    public static String arrayToString(int[] array) {
        StringBuilder sb = new StringBuilder();
        for (int value : array) {
            sb.append(value).append(" ");
        }
        return sb.toString().trim();
    }
}
